//
//  NotificationSender.m
//  Seizure
//
//  Created by student on 2/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NotificationSender.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"

static NotificationSender *sharedInstance = nil;

// Send location after seizure has been detected
@implementation NotificationSender
@synthesize locationManager;
@synthesize bestLocation;
BOOL sendingAlert;

// Get the shared instance and create it if necessary
+ (NotificationSender *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super alloc] init];
    }
    return sharedInstance;
}


- (id)init
{
    if (self){
        [self setupLocationManager];
        sendingAlert = NO;
    }
    return self;
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    static int MAX_AGE_OF_LOCATION = 60; // 10 seconds
    
    
    // age of the newLocation
    NSTimeInterval age = [newLocation.timestamp timeIntervalSinceNow];

    if (age < MAX_AGE_OF_LOCATION){
        //NSLog(@"Lat: %f. Log: %f. Accuracy %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.horizontalAccuracy);
        
        if (bestLocation == nil || bestLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
        {
            // new Location has a better accuracy than our current best
            bestLocation = [newLocation copy];
            
            //NSLog(@"Best Location Lat: %f. Log: %f. Accuracy %f", bestLocation.coordinate.latitude, bestLocation.coordinate.longitude, bestLocation.horizontalAccuracy);
        }
        else 
        {
            //NSLog(@"Old best location still work fine %f", [bestLocation coordinate].latitude);
        }
    }
    else{
        //NSLog(@"Location is older than 60 seconds");
    }
}

// get ready to send an alert with devices location
-(void) beginLocationTracking {
    sendingAlert = YES;
    [locationManager startUpdatingLocation];
    
}

- (void)stopLocationTracking
{
    sendingAlert = NO;
    [locationManager stopUpdatingLocation];
}

- (void)setupLocationManager
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
    
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    // Set a movement threshold for new events.
    locationManager.distanceFilter = kCLDistanceFilterNone;

}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    
    [locationManager stopUpdatingLocation];
}

// alert caretakers of patients status (exit early if using fake account)
- (void)sendLocationAlert
{
    DataManager *dm = [DataManager sharedInstance];
    if ([dm usingFakeAccount])
        return;
    [dm pushAlertCaretakersWithLocation:[self bestLocation]];
}


@end
